JBoss Community Archive (Read Only)

GateIn Portal 3.9

CDI Portlet Development under Tomcat

For GateIn users using Tomcat which does as being a barebones JSP/Servlet container. You’d need to install a CDI implementation which does support working under Tomcat.

In this documentation, we will guide to install Weld - a CDI reference implementation, as Weld documentation is pretty clear and easy to install under Tomcat.

Install Weld in GateIn Tomcat

Perform the following steps:

1. Drop the weld-servlet.jar in Tomcat common library $CATALINA_BASE/lib.

This is an uber JAR of Weld itself, contains both API and Impl. At the time of writing, weld-servlet 1.1.8.Final version is being used.

2. Create /META-INF/context.xml file in webapp's web content with following content (or, if you already have one, add just the <Resource> to it):

<Context>

  <Resource name="BeanManager"
            auth="Container"
            type="javax.enterprise.inject.spi.BeanManager"

            factory="org.jboss.weld.resources.ManagerObjectFactory" />

</Context>

This will register Weld's BeanManager factory in Tomcat's JNDI.

3. Add the following entries to webapp's /WEB-INF/web.xml:

<listener>
   <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
</listener>

<resource-env-ref>

   <resource-env-ref-name>BeanManager</resource-env-ref-name>

   <resource-env-ref-type>javax.enterprise.inject.spi.BeanManager</resource-env-ref-type>

</resource-env-ref>


The listener entry basically enables Weld in webapp. The resource entry basically tells webapp to use the BeanManager as available in JNDI.

There is a Weld issue when deploying compressed Archive to Tomcat which is reported at https://issues.jboss.org/browse/WELD-617, that beans can not be scanned properly. The issue even appears when you start Tomcat with unpackWars="false".

In this case, you will need to use the custom org.jboss.weld.environment.servlet.TomcatListener instead of org.jboss.weld.environment.servlet.Listener

Portlet CDI scopes

As you know, GateIn has provided two Portlet CDI scope annotations @PortletLifecycleScoped and @PortletRedisplayScoped which support the complex Portlet Lifecycle within CDI. BUT if you want to have them work under Tomcat, you also need to explicitly specify the following servlet listener in the webapp's /WEB-INF/web.xml

<listener>
   <listener-class>org.gatein.cdi.contexts.listeners.TomcatCDIServletListener</listener-class>
</listener>
JBoss.org Content Archive (Read Only), exported from JBoss Community Documentation Editor at 2020-03-10 13:29:29 UTC, last content change 2014-08-13 07:34:46 UTC.